package com.eight.system.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.AclUser;
import com.eight.common.vo.Result;
import com.eight.system.service.UserService;
import com.eight.system.vo.AclUserVo;
import com.eight.system.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.concurrent.TimeUnit;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/22 20:30
 */
@RestController
@RequestMapping("/system/user")
@Api("用户权限管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/login")
    @ApiOperation(value = "登录接口")
    public Result login(@RequestBody LoginVo loginVo){
        AclUser aclUser = userService.FindByNameAndPwd(loginVo.getUsername(), loginVo.getPassword());
        System.out.println(aclUser);
        if(aclUser!=null){
            if(aclUser.getIsDeleted().equals(0)){
                //表示成功
                String token = IdUtil.simpleUUID();
                System.out.println(token+" 111111111");
                aclUser.setPassword(null);
                redisTemplate.opsForValue().set("user::"+token, JSON.toJSONString(aclUser),30, TimeUnit.MINUTES);
                return Result.success(token);
            }else{
                //表示该账号已经被冻结
                return Result.fail("该账号被冻结了，请联系管理员");
            }
        }
        return Result.fail("账号或密码错误");
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取当前用户信息")
    public Result info(HttpServletRequest request){
        //1.从请求头获取token信息
        String token = request.getHeader("token");
        //2.从redis中获取当前登录者的信息
        //request.getAttribute()
        String json = redisTemplate.opsForValue().get("user::" + token);
        AclUser user = JSON.parseObject(json, AclUser.class);
        return Result.success(user);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Result logout(HttpServletRequest request){
        //1.从请求头获取token信息
        String token = request.getHeader("token");
        Boolean delete = redisTemplate.delete("user::" + token);
        return Result.success();
    }


    @ApiOperation("分页条件查询")
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result list(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody AclUserVo userVo){
        Page<AclUser> userPage = userService.findPage(currentPage, pageSize, userVo);
        return Result.success(userPage);
    }

    @ApiOperation("修改用户状态")
    @PostMapping("/status")
    public Result status(@RequestParam String id , @RequestParam Integer status){
        int row=userService.updateStatus(id,status);
        return Result.success();
    }

    @ApiOperation("根据ID删除用户")
    @DeleteMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable String id){
        int userById = userService.deleteUserById(id);
        return Result.success();
    }
    @ApiOperation("修改备注")
    @PostMapping("/updateUserByUser")
    public Result updateUserByUser(@RequestBody AclUser aclUser){
        int i = userService.updateUserByUser(aclUser);
        return Result.success(i);
    }
    @ApiOperation("添加用户")
    @PostMapping("/addUserByUser")
    public Result addUserByUser(@RequestBody AclUser aclUser){
        int i = userService.addUserByUser(aclUser);
        return Result.success(i);
    }
    @ApiOperation("修改密码")
    @PostMapping("updateUserPassword/{oldPassword}/{newPassword}")
    public Result updateUserPassword(HttpServletRequest httpServletRequest,@PathVariable String oldPassword,@PathVariable String newPassword){
        String token = httpServletRequest.getHeader("token");
        String json = redisTemplate.opsForValue().get("user::" + token);
        JSONObject object = JSONObject.parseObject(json);
        String id = (String)object.get("id");
        int updatePassword = userService.updateUserPassword(id, oldPassword, newPassword);
        if (updatePassword==0){
            return Result.fail("原密码错误!");
        }
        return Result.success();
    }
}
